def modify_archlinux_sources():
    path = "/etc/pacman.d/mirrorlist"
    # 清空文件内容
    # path = "./tmp1.conf"
    with open(path, "w") as file:
        pass

    # 添加USTC和清华源配置
    with open(path, "a") as file:
        file.write(
            "Server = https://mirrors.ustc.edu.cn/archlinux/$repo/os/$arch\n"
            # "Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch\n"
            # "Server = https://mirror.nju.edu.cn/manjaro/stable/$repo/$arch\n"
            # "Server = https://mirror.nyist.edu.cn/manjaro/stable/$repo/$arch\n"
        )


def archlinuxcn_source():
    path1 = "/etc/pacman.conf"
    # path1 = "./tmp.conf"
    text = "SigLevel"
    with open(path1, "r") as file:
        lines = file.readlines()
        for i, line in enumerate(lines):
            if text in line:
                lines[i] = "SigLevel = Never\n"
                break
        with open(path1, "w") as file:
            file.writelines(lines)

    text = "Color"
    with open(path1, "r") as file:
        lines = file.readlines()
        for i, line in enumerate(lines):
            if text in line:
                lines[i] = "Color\n"
                lines.insert(i + 1, "ILoveCandy\n")

                break
        with open(path1, "w") as file:
            file.writelines(lines)

    text = "[archlinuxcn]"
    with open(path1, "r") as file:
        content = file.read()
        if text in content:
            pass
        else:
            with open(path1, "a") as file:
                file.write(
                    "\n[archlinuxcn]\nServer = https://mirrors.ustc.edu.cn/archlinuxcn/$arch"
                )


def main():
    modify_archlinux_sources()
    archlinuxcn_source()


if __name__ == "__main__":
    main()
